/*
* INSANE - Interactive Structural Analysis Environment
*
* Copyright (C) 2003-2005
* Universidade Federal de Minas Gerais
* Escola de Engenharia
* Departamento de Engenharia de Estruturas
* 
* Author's email :    insane@dees.ufmg.br
* Author's website :  http://www.dees.ufmg.br/insane
* 
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
package br.ufmg.dees.insane.ui.rich.full.dialogs;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import br.ufmg.dees.insane.commons.dialogs.SimpleDialog;
import br.ufmg.dees.insane.model.ModelState;
import br.ufmg.dees.insane.ui.rich.full.InsaneInternalInterface;
import br.ufmg.dees.insane.ui.rich.full.command.LastZoomCommand;

/**
 * A class representing creation dialog on screen.
 *
 * @author Fonseca, Flavio & Pitangueira, Roque
 * @see gui.InsaneInternalInterface
 * @since July 2004
 */
public final class DiagramsSettingsDialog extends SimpleDialog {

	private static final long serialVersionUID = 1L;
	private InsaneInternalInterface parent;
    private JPanel diagramsPane = new JPanel();
	private JPanel diaColorsPane = new JPanel();
    
	private JTextField fxDiagramColorField = new JTextField();
	private JTextField fyDiagramColorField = new JTextField();
	private JTextField fzDiagramColorField = new JTextField();
	private JTextField mxDiagramColorField = new JTextField();
	private JTextField myDiagramColorField = new JTextField();
	private JTextField mzDiagramColorField = new JTextField();
	private JTextField dxDiagramColorField = new JTextField();
	private JTextField dyDiagramColorField = new JTextField();
	private JTextField dzDiagramColorField = new JTextField();
	private JTextField rxDiagramColorField = new JTextField();
	private JTextField ryDiagramColorField = new JTextField();
	private JTextField rzDiagramColorField = new JTextField();
	
	private JButton chooseFxDiagramColor = new JButton();
	private JButton chooseFyDiagramColor = new JButton();
	private JButton chooseFzDiagramColor = new JButton();
	private JButton chooseMxDiagramColor = new JButton();
	private JButton chooseMyDiagramColor = new JButton();
	private JButton chooseMzDiagramColor = new JButton();
	private JButton chooseDxDiagramColor = new JButton();
	private JButton chooseDyDiagramColor = new JButton();
	private JButton chooseDzDiagramColor = new JButton();
	private JButton chooseRxDiagramColor = new JButton();
	private JButton chooseRyDiagramColor = new JButton();
	private JButton chooseRzDiagramColor = new JButton();
    
    private ModelState state;
	
//*****************************************************************************
	
    /**
     * Constructor method, calling super with the especific title.
     *
     * @param parent The parent of the class.
     * @see gui.Interface
     */
    public DiagramsSettingsDialog(InsaneInternalInterface parent, ModelState s) {
        this.parent = parent;
        state = s;
		
		chooseFxDiagramColor.setText(parent.getResourceBundle().getString("gChooseButton"));
		chooseFyDiagramColor.setText(parent.getResourceBundle().getString("gChooseButton"));
		chooseFzDiagramColor.setText(parent.getResourceBundle().getString("gChooseButton"));
		chooseMxDiagramColor.setText(parent.getResourceBundle().getString("gChooseButton"));
		chooseMyDiagramColor.setText(parent.getResourceBundle().getString("gChooseButton"));
		chooseMzDiagramColor.setText(parent.getResourceBundle().getString("gChooseButton"));
		chooseDxDiagramColor.setText(parent.getResourceBundle().getString("gChooseButton"));
		chooseDyDiagramColor.setText(parent.getResourceBundle().getString("gChooseButton"));
		chooseDzDiagramColor.setText(parent.getResourceBundle().getString("gChooseButton"));
		chooseRxDiagramColor.setText(parent.getResourceBundle().getString("gChooseButton"));
		chooseRyDiagramColor.setText(parent.getResourceBundle().getString("gChooseButton"));
		chooseRzDiagramColor.setText(parent.getResourceBundle().getString("gChooseButton"));
		
		//Sets internal diagrams colors panel
        diaColorsPane.setLayout(new GridLayout(12,3,2,2));
        diaColorsPane.setBorder(BorderFactory.createCompoundBorder(
        BorderFactory.createTitledBorder(parent.getResourceBundle().getString("dDiaDialog02")),
        BorderFactory.createEmptyBorder(2,2,2,2)));
		diaColorsPane.setMaximumSize(new Dimension(500,400));
		diaColorsPane.add(new JLabel(parent.getResourceBundle().getString("dDiaDialog03")));
        fxDiagramColorField.setEditable(false);
        fxDiagramColorField.setBackground(state.fxDiagramColor);
        diaColorsPane.add(fxDiagramColorField);
        diaColorsPane.add(chooseFxDiagramColor);
        chooseFxDiagramColor.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                fxDiagramColorActionPerformed(e);
            }
        } );
		diaColorsPane.add(new JLabel(parent.getResourceBundle().getString("dDiaDialog04")));
        fyDiagramColorField.setEditable(false);
        fyDiagramColorField.setBackground(state.fyDiagramColor);
        diaColorsPane.add(fyDiagramColorField);
        diaColorsPane.add(chooseFyDiagramColor);
        chooseFyDiagramColor.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                fyDiagramColorActionPerformed(e);
            }
        } );
		diaColorsPane.add(new JLabel(parent.getResourceBundle().getString("dDiaDialog05")));
        fzDiagramColorField.setEditable(false);
        fzDiagramColorField.setBackground(state.fzDiagramColor);
        diaColorsPane.add(fzDiagramColorField);
        diaColorsPane.add(chooseFzDiagramColor);
        chooseFzDiagramColor.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                fzDiagramColorActionPerformed(e);
            }
        } );
		diaColorsPane.add(new JLabel(parent.getResourceBundle().getString("dDiaDialog06")));
        mxDiagramColorField.setEditable(false);
        mxDiagramColorField.setBackground(state.mxDiagramColor);
        diaColorsPane.add(mxDiagramColorField);
        diaColorsPane.add(chooseMxDiagramColor);
        chooseMxDiagramColor.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                mxDiagramColorActionPerformed(e);
            }
        } );
		diaColorsPane.add(new JLabel(parent.getResourceBundle().getString("dDiaDialog07")));
        myDiagramColorField.setEditable(false);
        myDiagramColorField.setBackground(state.myDiagramColor);
        diaColorsPane.add(myDiagramColorField);
        diaColorsPane.add(chooseMyDiagramColor);
        chooseMyDiagramColor.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                myDiagramColorActionPerformed(e);
            }
        } );
		diaColorsPane.add(new JLabel(parent.getResourceBundle().getString("dDiaDialog08")));
        mzDiagramColorField.setEditable(false);
        mzDiagramColorField.setBackground(state.mzDiagramColor);
        diaColorsPane.add(mzDiagramColorField);
        diaColorsPane.add(chooseMzDiagramColor);
        chooseMzDiagramColor.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                mzDiagramColorActionPerformed(e);
            }
        } );
		diaColorsPane.add(new JLabel(parent.getResourceBundle().getString("dDiaDialog09")));
        dxDiagramColorField.setEditable(false);
        dxDiagramColorField.setBackground(state.dxDiagramColor);
        diaColorsPane.add(dxDiagramColorField);
        diaColorsPane.add(chooseDxDiagramColor);
        chooseDxDiagramColor.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                dxDiagramColorActionPerformed(e);
            }
        } );
		diaColorsPane.add(new JLabel(parent.getResourceBundle().getString("dDiaDialog10")));
        dyDiagramColorField.setEditable(false);
        dyDiagramColorField.setBackground(state.dyDiagramColor);
        diaColorsPane.add(dyDiagramColorField);
        diaColorsPane.add(chooseDyDiagramColor);
        chooseDyDiagramColor.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                dyDiagramColorActionPerformed(e);
            }
        } );
		diaColorsPane.add(new JLabel(parent.getResourceBundle().getString("dDiaDialog11")));
        dzDiagramColorField.setEditable(false);
        dzDiagramColorField.setBackground(state.dzDiagramColor);
        diaColorsPane.add(dzDiagramColorField);
        diaColorsPane.add(chooseDzDiagramColor);
        chooseDzDiagramColor.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                dzDiagramColorActionPerformed(e);
            }
        } );
		diaColorsPane.add(new JLabel(parent.getResourceBundle().getString("dDiaDialog12")));
        rxDiagramColorField.setEditable(false);
        rxDiagramColorField.setBackground(state.rxDiagramColor);
        diaColorsPane.add(rxDiagramColorField);
        diaColorsPane.add(chooseRxDiagramColor);
        chooseRxDiagramColor.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                rxDiagramColorActionPerformed(e);
            }
        } );
		diaColorsPane.add(new JLabel(parent.getResourceBundle().getString("dDiaDialog13")));
        ryDiagramColorField.setEditable(false);
        ryDiagramColorField.setBackground(state.ryDiagramColor);
        diaColorsPane.add(ryDiagramColorField);
        diaColorsPane.add(chooseRyDiagramColor);
        chooseRyDiagramColor.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                ryDiagramColorActionPerformed(e);
            }
        } );
		diaColorsPane.add(new JLabel(parent.getResourceBundle().getString("dDiaDialog14")));
        rzDiagramColorField.setEditable(false);
        rzDiagramColorField.setBackground(state.rzDiagramColor);
        diaColorsPane.add(rzDiagramColorField);
        diaColorsPane.add(chooseRzDiagramColor);
        chooseRzDiagramColor.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                rzDiagramColorActionPerformed(e);
            }
        } );
		
		//Sets diagrams panel
        diagramsPane.setLayout(new BoxLayout(diagramsPane, BoxLayout.Y_AXIS));
        diagramsPane.add(diaColorsPane);
		
        setPane(diagramsPane);
        
    }
	
//*****************************************************************************
    
    public int showDialog() {
        return super.showDialog(parent, parent.getResourceBundle().getString("dDiaDialog01"));
    }
	
//*****************************************************************************
	
    /**
     * This method is executed on OK button click.
     *
     */
    public void approvedPerform() {
        try {
            
			state.fxDiagramColor = fxDiagramColorField.getBackground();
			state.fyDiagramColor = fyDiagramColorField.getBackground();
			state.fzDiagramColor = fzDiagramColorField.getBackground();
			state.mxDiagramColor = mxDiagramColorField.getBackground();
			state.myDiagramColor = myDiagramColorField.getBackground();
			state.mzDiagramColor = mzDiagramColorField.getBackground();
			state.dxDiagramColor = dxDiagramColorField.getBackground();
			state.dyDiagramColor = dyDiagramColorField.getBackground();
			state.dzDiagramColor = dzDiagramColorField.getBackground();
			state.rxDiagramColor = rxDiagramColorField.getBackground();
			state.ryDiagramColor = ryDiagramColorField.getBackground();
			state.rzDiagramColor = rzDiagramColorField.getBackground();
            
			parent.setModel(parent.getModel());
            this.setVisible(false);
			
			LastZoomCommand cmd = new LastZoomCommand(parent);
			cmd.execute();
			
        } catch (Exception err) {
            Toolkit.getDefaultToolkit().beep();
            this.setVisible(false);
        }
    }
    
//*****************************************************************************
	
	private void fxDiagramColorActionPerformed(ActionEvent e) {
        fxDiagramColorField.setBackground(JColorChooser.showDialog(this, parent.getResourceBundle().getString("dDiaDialog15"), fxDiagramColorField.getBackground()));
    }
	
//*****************************************************************************
	
	private void fyDiagramColorActionPerformed(ActionEvent e) {
        fyDiagramColorField.setBackground(JColorChooser.showDialog(this, parent.getResourceBundle().getString("dDiaDialog16"), fyDiagramColorField.getBackground()));
    }
	
//*****************************************************************************
	
	private void fzDiagramColorActionPerformed(ActionEvent e) {
        fzDiagramColorField.setBackground(JColorChooser.showDialog(this, parent.getResourceBundle().getString("dDiaDialog17"), fzDiagramColorField.getBackground()));
    }
	
//*****************************************************************************
	
	private void mxDiagramColorActionPerformed(ActionEvent e) {
        mxDiagramColorField.setBackground(JColorChooser.showDialog(this, parent.getResourceBundle().getString("dDiaDialog18"), mxDiagramColorField.getBackground()));
    }
	
//*****************************************************************************
	
	private void myDiagramColorActionPerformed(ActionEvent e) {
        myDiagramColorField.setBackground(JColorChooser.showDialog(this, parent.getResourceBundle().getString("dDiaDialog19"), myDiagramColorField.getBackground()));
    }
	
//*****************************************************************************
	
	private void mzDiagramColorActionPerformed(ActionEvent e) {
        mzDiagramColorField.setBackground(JColorChooser.showDialog(this, parent.getResourceBundle().getString("dDiaDialog20"), mzDiagramColorField.getBackground()));
    }
	
//*****************************************************************************
	
	private void dxDiagramColorActionPerformed(ActionEvent e) {
        dxDiagramColorField.setBackground(JColorChooser.showDialog(this, parent.getResourceBundle().getString("dDiaDialog21"), dxDiagramColorField.getBackground()));
    }
	
//*****************************************************************************
	
	private void dyDiagramColorActionPerformed(ActionEvent e) {
        dyDiagramColorField.setBackground(JColorChooser.showDialog(this, parent.getResourceBundle().getString("dDiaDialog22"), dyDiagramColorField.getBackground()));
    }
	
//*****************************************************************************
	
	private void dzDiagramColorActionPerformed(ActionEvent e) {
        dzDiagramColorField.setBackground(JColorChooser.showDialog(this, parent.getResourceBundle().getString("dDiaDialog23"), dzDiagramColorField.getBackground()));
    }
	
//*****************************************************************************
	
	private void rxDiagramColorActionPerformed(ActionEvent e) {
        rxDiagramColorField.setBackground(JColorChooser.showDialog(this, parent.getResourceBundle().getString("dDiaDialog24"), rxDiagramColorField.getBackground()));
    }
	
//*****************************************************************************
	
	private void ryDiagramColorActionPerformed(ActionEvent e) {
        ryDiagramColorField.setBackground(JColorChooser.showDialog(this, parent.getResourceBundle().getString("dDiaDialog25"), ryDiagramColorField.getBackground()));
    }
	
//*****************************************************************************
	
	private void rzDiagramColorActionPerformed(ActionEvent e) {
        rzDiagramColorField.setBackground(JColorChooser.showDialog(this, parent.getResourceBundle().getString("dDiaDialog26"), rzDiagramColorField.getBackground()));
    }
	
//*****************************************************************************
}